package com.xyy.saas.payment.service.impl;

import com.xyy.saas.payment.dao.mapper.PayAmountReportMapper;
import com.xyy.saas.payment.dao.model.PayAmountReport;
import com.xyy.saas.payment.dao.model.PayAmountReportQueryParam;
import com.xyy.saas.payment.service.PayAmountReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class PayAmountReportServiceImpl implements PayAmountReportService {

    @Autowired
    private PayAmountReportMapper payAmountReportMapper;

    @Override
    public PayAmountReport insert(PayAmountReport entity) {
        payAmountReportMapper.insert(entity);
        return entity;
    }

    @Override
    public List<PayAmountReport> pageByParam(PayAmountReportQueryParam queryParam) {
        if(null == queryParam) {
            return new ArrayList<>();
        }
        PayAmountReport param = new PayAmountReport();
        if(!StringUtils.isEmpty(queryParam.getBusinessType())) {
            param.setBusinessType(queryParam.getBusinessType());
        }
        if(!StringUtils.isEmpty(queryParam.getBusinessOrderType())) {
            param.setBusinessOrderType(queryParam.getBusinessOrderType());
        }
        if(!StringUtils.isEmpty(queryParam.getPayChannel())) {
            param.setPayChannel(queryParam.getPayChannel());
        }
        String startTime = null;
        String endTime = null;
        if(!StringUtils.isEmpty(queryParam.getStartTime())) {
            startTime = queryParam.getStartTime();
        }
        if(!StringUtils.isEmpty(queryParam.getEndTime())) {
            endTime = queryParam.getEndTime();
        }
        int offset = 0;
        int limit = 100;
        if(queryParam.getOffset() > 0) {
            offset = queryParam.getOffset();
        }
        if(queryParam.getLimit() > 0 && queryParam.getLimit() <= 100) {
            limit = queryParam.getLimit();
        }
        return payAmountReportMapper.pageByParam(param, startTime, endTime, offset, limit);
    }

    @Override
    public List<PayAmountReport> pageByGroupParam(PayAmountReportQueryParam queryParam) {
        if(null == queryParam) {
            return new ArrayList<>();
        }
        PayAmountReport param = new PayAmountReport();
        if(!StringUtils.isEmpty(queryParam.getBusinessType())) {
            param.setBusinessType(queryParam.getBusinessType());
        }
        if(!StringUtils.isEmpty(queryParam.getBusinessOrderType())) {
            param.setBusinessOrderType(queryParam.getBusinessOrderType());
        }
        if(!StringUtils.isEmpty(queryParam.getPayChannel())) {
            param.setPayChannel(queryParam.getPayChannel());
        }
        String startTime = null;
        String endTime = null;
        if(!StringUtils.isEmpty(queryParam.getStartTime())) {
            startTime = queryParam.getStartTime();
        }
        if(!StringUtils.isEmpty(queryParam.getEndTime())) {
            endTime = queryParam.getEndTime();
        }
        int offset = 0;
        int limit = 100;
        if(queryParam.getOffset() > 0) {
            offset = queryParam.getOffset();
        }
        if(queryParam.getLimit() > 0 && queryParam.getLimit() <= 100) {
            limit = queryParam.getLimit();
        }
        return payAmountReportMapper.pageByGroupParam(param, startTime, endTime, offset, limit);
    }

    @Override
    public List<PayAmountReport> queryByParam(PayAmountReportQueryParam queryParam) {

        if(null == queryParam) {
            return new ArrayList<>();
        }
        PayAmountReport param = new PayAmountReport();
        if(!StringUtils.isEmpty(queryParam.getBusinessType())) {
            param.setBusinessType(queryParam.getBusinessType());
        }
        if(!StringUtils.isEmpty(queryParam.getBusinessOrderType())) {
            param.setBusinessOrderType(queryParam.getBusinessOrderType());
        }
        if(!StringUtils.isEmpty(queryParam.getPayChannel())) {
            param.setPayChannel(queryParam.getPayChannel());
        }
        String startTime = null;
        String endTime = null;
        if(!StringUtils.isEmpty(queryParam.getStartTime())) {
            startTime = queryParam.getStartTime();
        }
        if(!StringUtils.isEmpty(queryParam.getEndTime())) {
            endTime = queryParam.getEndTime();
        }
        return payAmountReportMapper.queryByParam(param, startTime, endTime);
    }

    @Override
    public long getTotal(PayAmountReportQueryParam queryParam) {

        if(null == queryParam) {
            return 0l;
        }
        PayAmountReport param = new PayAmountReport();
        if(!StringUtils.isEmpty(queryParam.getBusinessType())) {
            param.setBusinessType(queryParam.getBusinessType());
        }
        if(!StringUtils.isEmpty(queryParam.getBusinessOrderType())) {
            param.setBusinessOrderType(queryParam.getBusinessOrderType());
        }
        if(!StringUtils.isEmpty(queryParam.getPayChannel())) {
            param.setPayChannel(queryParam.getPayChannel());
        }
        String startTime = null;
        String endTime = null;
        if(!StringUtils.isEmpty(queryParam.getStartTime())) {
            startTime = queryParam.getStartTime();
        }
        if(!StringUtils.isEmpty(queryParam.getEndTime())) {
            endTime = queryParam.getEndTime();
        }
        return payAmountReportMapper.getTotal(param, startTime, endTime);
    }

    @Override
    public int updateAmountById(Long id, int amount) {
        return payAmountReportMapper.updateAmountById(id, amount);
    }
}
